package com.fieldchain.ex.fa100.utils.common;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.MyApplication;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/8/20
 */
public class VerticalDisUtils {
    private static VerticalDisUtils ver;
    private TextView tv;
    private RelativeLayout rel;
    private Timer timer;
    private int tags = 60;
    private int c1, c2;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (tags > 0) {
                        if (tags == 60) {
                            rel.setVisibility(View.VISIBLE);
                            tv.setTextColor(c2);
                        }
                        tv.setText(tags + "s");
                        tags--;
                    } else {
                        tv.setClickable(true);
                        rel.setVisibility(View.GONE);
                        timer.cancel();
                        tv.setTextColor(c1);
                        tv.setText(MyApplication.getAppContext().getResources().getText(R.string.get_code));
                    }
                    break;
            }
        }
    };
    public void stop(){
        if(timer!=null){
            timer.cancel();
        }
        mHandler.removeCallbacksAndMessages(null);
        if(tv!=null){
            tv.setClickable(true);
            rel.setVisibility(View.GONE);
            timer.cancel();
            tv.setTextColor(c1);
            tv.setText(MyApplication.getAppContext().getResources().getText(R.string.get_code));
        }
    }
    private VerticalDisUtils() {
        c2= MyApplication.getAppContext().getResources().getColor(R.color.text_defaut);
        c1=MyApplication.getAppContext().getResources().getColor(R.color.text_input);
    }

    public static VerticalDisUtils getInstance() {
        if (null == ver) ver = new VerticalDisUtils();
        return ver;
    }
    public void start(final EditText et, TextView tv, RelativeLayout rel) {
        tags = 60;
        this.tv = tv;
        this.rel = rel;
        tv.setClickable(false);
        startTimer();
        rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               et.setText("");
            }
        });
    }

    public void cancle() {
        if (timer != null) {
            timer.cancel();
        }
        mHandler.removeCallbacksAndMessages(null);
        if (tv != null && tv.getContext() != null) {
            tv.setTextColor(c1);
            rel.setVisibility(View.GONE);
            tv.setText(MyApplication.getAppContext().getResources().getText(R.string.get_code));
        }
        tv=null;
        rel=null;
    }

    private void startTimer() {
        if (timer != null) {
            timer = null;
        }
        timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(0);
            }
        };
        timer.schedule(task, 0, 1000);
    }

}
